﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class CalendarQueryManager_Index : System.Web.UI.Page {
    private int curYear;
    private int curMonth;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            curYear = DateTime.Now.Year;
            curMonth = DateTime.Now.Month;
            for(int i = 1958; i < 2058; i++) {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                if(i == curYear) {
                    item.Selected = true;
                }
                this.DdlYear.Items.Add(item);
            }
            for(int i = 1; i < 13; i++) {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                if(i == curMonth) {
                    item.Selected = true;
                }
                this.DdlMonth.Items.Add(item);
            }
            AddSections();
            AddDateText();
            ViewState["curYear"] = curYear;
            ViewState["curMonth"] = curMonth;
        }
    }

    protected void AddSections() {
        SectionServices sectionService = new SectionServices();
        List<Section> list = sectionService.findAllSection();
        if(list != null && list.Count > 0) {
            foreach(Section section in list) {
                this.DdlSections.Items.Add(new ListItem(section.SectionName, section.SectionId.ToString()));
            }
        }
    }

    private void AddDateText() {
        string s_id = this.DdlSections.SelectedItem.Value;
        User curLoginUser = (User) Session["user"];
        for(int i = 1; i < 43; i++) {
            ((Label) this.FindControl("Label" + i)).Text = "";
        }
        DateTime dt;
        int d;
        if(curMonth < 1) {
            curYear--;
            curMonth = 12;
            dt = DateTime.Parse(curYear + "-" + (curMonth - 1) + "-" + DateTime.DaysInMonth(curYear, curMonth - 1));

        } else if(curMonth == 1) {
            dt = DateTime.Parse((curYear - 1) + "-" + 12 + "-" + DateTime.DaysInMonth((curYear - 1), 12));
        } else if(curMonth > 12) {
            dt = DateTime.Parse(curYear + "-" + 12 + "-" + DateTime.DaysInMonth(curYear, 12));
            curYear++;
            curMonth = 1;
        } else {
            dt = DateTime.Parse(curYear + "-" + (curMonth - 1) + "-" + DateTime.DaysInMonth(curYear, curMonth - 1));
        }
        d = DateTime.DaysInMonth(curYear, curMonth);
        int start = 1;
        if(dt.AddDays(1).DayOfWeek == DayOfWeek.Monday) {
            start = 2;
        } else if(dt.AddDays(1).DayOfWeek == DayOfWeek.Tuesday) {
            start = 3;
        } else if(dt.AddDays(1).DayOfWeek == DayOfWeek.Wednesday) {
            start = 4;
        } else if(dt.AddDays(1).DayOfWeek == DayOfWeek.Thursday) {
            start = 5;
        } else if(dt.AddDays(1).DayOfWeek == DayOfWeek.Friday) {
            start = 6;
        } else if(dt.AddDays(1).DayOfWeek == DayOfWeek.Saturday) {
            start = 7;
        }
        for(int i = 1; i <= d; i++) {
            DateTime ddt = DateTime.Parse(curYear + "-" + curMonth + "-" + i.ToString());
            if(i == DateTime.Now.Day && curYear == DateTime.Now.Year && curMonth == DateTime.Now.Month) {
                ((Label) this.FindControl("Label" + start)).Text = "<a style='text-decoration: none' target='bottomFrame' href='UserDiaryShow.aspx?s_id=" + s_id + "&dt_st=" +
                    curYear + "/" + curMonth + "/" + i.ToString() + "'><font style='font-size:20px'><b>" + i.ToString() + "</b></font></a>";
            } else if(ddt.DayOfWeek == DayOfWeek.Saturday || ddt.DayOfWeek == DayOfWeek.Sunday) {
                ((Label) this.FindControl("Label" + start)).Text = "<a style='text-decoration: none' target='bottomFrame' href='UserDiaryShow.aspx?s_id=" + s_id + "&dt_st=" +
                    curYear + "/" + curMonth + "/" + i.ToString() + "'><font color='red'><b>" + i.ToString() + "</b></font></a>";
            } else {
                ((Label) this.FindControl("Label" + start)).Text = "<a style='text-decoration: none' target='bottomFrame' href='UserDiaryShow.aspx?s_id=" + s_id + "&dt_st=" + curYear + "/" + curMonth +
                    "/" + i.ToString() + "'>" + i.ToString() + "</a>";
            }
            start++;
        }
        if(curMonth == DateTime.Now.Month && curYear == DateTime.Now.Year) {
            this.Label43.Text = "<font>" + curMonth.ToString() + "月</font>";
            this.Label44.Text = "<font>" + curYear.ToString() + "年</font>";
        } else {
            this.Label43.Text = curMonth.ToString() + "月";
            this.Label44.Text = curYear.ToString() + "年";
        }
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }

    protected void DdlYear_SelectedIndexChanged(object sender, EventArgs e) {
        if(ViewState["curMonth"] != null && ViewState["curYear"] != null) {
            curYear = (int) ViewState["curYear"];
            curMonth = (int) ViewState["curMonth"];
        }
        curYear = int.Parse(Request.Params.GetValues(this.DdlYear.ClientID)[0]);
        curMonth = int.Parse(Request.Params.GetValues(this.DdlMonth.ClientID)[0]);
        AddDateText();
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }

    protected void DdlMonth_SelectedIndexChanged(object sender, EventArgs e) {
        if(ViewState["curMonth"] != null && ViewState["curYear"] != null) {
            curYear = (int) ViewState["curYear"];
            curMonth = (int) ViewState["curMonth"];
        }
        curYear = int.Parse(Request.Params.GetValues(this.DdlYear.ClientID)[0]);
        curMonth = int.Parse(Request.Params.GetValues(this.DdlMonth.ClientID)[0]);
        AddDateText();
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }

    protected void Button1_Click(object sender, EventArgs e) {
        if(ViewState["curMonth"] != null && ViewState["curYear"] != null) {
            curMonth = (int) ViewState["curMonth"];
            curYear = (int) ViewState["curYear"];
        }
        curMonth--;
        AddDateText();
        ViewState["curMonth"] = curMonth;
        ViewState["curYear"] = curYear;
    }

    protected void Button2_Click(object sender, EventArgs e) {
        if(ViewState["curMonth"] != null && ViewState["curYear"] != null) {
            curMonth = (int) ViewState["curMonth"];
            curYear = (int) ViewState["curYear"];
        }
        curMonth++;
        AddDateText();
        ViewState["curMonth"] = curMonth;
        ViewState["curYear"] = curYear;
    }

    protected void Button3_Click(object sender, EventArgs e) {
        if(ViewState["curYear"] != null && ViewState["curMonth"] != null) {
            curMonth = (int) ViewState["curMonth"];
            curYear = (int) ViewState["curYear"];
        }
        curYear--;
        AddDateText();
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }

    protected void Button4_Click(object sender, EventArgs e) {
        if(ViewState["curYear"] != null && ViewState["curMonth"] != null) {
            curMonth = (int) ViewState["curMonth"];
            curYear = (int) ViewState["curYear"];
        }
        curYear++;
        AddDateText();
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }
    protected void DdlSections_SelectedIndexChanged(object sender, EventArgs e) {
        if(ViewState["curYear"] != null && ViewState["curMonth"] != null) {
            curYear = (int) ViewState["curYear"];
            curMonth = (int) ViewState["curMonth"];
        }
        this.AddDateText();
        ViewState["curYear"] = curYear;
        ViewState["curMonth"] = curMonth;
    }
}
